package im.vinci.server.user.domain.sms;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.google.common.collect.ImmutableMap;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import im.vinci.server.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by mingjie on 17/3/28.
 */
@Component
public class SmsSendByYunpian implements ISmsSend {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private final String API_KEY = "29fd611ffa792e9694fae1c6d439ca06";

    @Override
    public boolean sendTextSms(String mobileNo, String msg) {
        try {
            ImmutableMap<String, Object> map = ImmutableMap.<String, Object>builder().put("apikey", API_KEY)
                    .put("mobile", mobileNo).put("text", msg).build();
            HttpResponse<String> response = Unirest.post("http://yunpian.com/v1/sms/send.json").fields(map).asString();
            if (response.getStatus() != 200) {
                logger.warn("send yunpian sms failed:{},{}", response.getStatus(), response.getStatusText());
                return false;
            }
            // success: {"code":0,"msg":"OK","result":{"count":1,"fee":1,"sid":5468025326}}
            // failed : {"code":2,"msg":"请求参数格式错误","detail":"参数 mobile 格式不正确，mobile手机号格式不正确"}
            JsonNode root = JsonUtils.decode(response.getBody(), JsonNode.class);
            if (root == null || root.findValue("code") == null ||
                    root.findValue("code").getNodeType() != JsonNodeType.NUMBER ||
                    root.findValue("code").asInt() != 0) {
                logger.warn("send yunpian sms failed:{}", response.getBody());
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean sendAudioSms(String mobileNo, String msg) {
        try {
            ImmutableMap<String, Object> map = ImmutableMap.<String, Object>builder().put("apikey", API_KEY)
                    .put("mobile", mobileNo).put("code", msg).build();
            HttpResponse<String> response = Unirest.post("https://voice.yunpian.com/v2/voice/send.json").fields(map).asString();
            if (response.getStatus() != 200) {
                logger.warn("send yunpian audio failed:{},{}", response.getStatus(), response.getStatusText());
                return false;
            }

            JsonNode root = JsonUtils.decode(response.getBody(), JsonNode.class);
            if (root == null || (root.findValue("code") != null &&
                    (root.findValue("code").getNodeType() != JsonNodeType.NUMBER ||
                            root.findValue("code").asInt() != 0))) {
                logger.warn("send yunpian sms failed:{}", response.getBody());
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        boolean result = new SmsSendByYunpian().sendAudioSms("15902207909", "32875");
        System.out.println("send msg result:" + result);
    }
}